package com.rfid.assistant.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

//import com.rfid.config.Config;
import com.rfid.sockethelper.ClientManager;
import com.rfid.sockethelper.ClientSocketHelper;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

public class SendActivity extends SRBaseActivity {


    public static void startActivity(Context context, String ip) {
        startActivity(context,null,ip,false);
    }

    public static void startActivity(Context context, Uri uri, String ip) {
        startActivity(context,uri,ip,false);
    }

    public static void startActivity(Context context, Uri uri, String ip,boolean isBlue) {
        System.out.println("startActivity uri = " + uri);
        Intent intent = new Intent();
        intent.putExtra("bluetooth",isBlue);
        intent.putExtra("ip",ip);
        intent.setData(uri);
        intent.setClass(context,SendActivity.class);
        context.startActivity(intent);
    }

    Uri uri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uri = getIntent().getData();
        System.out.println("uri = " +uri);
        mIp = getIntent().getStringExtra("ip");
        ClientManager.getManager().addListener(this);
        if (uri != null) {
            new AlertDialog.Builder(this).setMessage("文件是否发送到指定目录？")
                    .setNegativeButton("否，发送到默认目录", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            handleSelectedFile(uri);
                        }
                    })
                    .setPositiveButton("是", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            sendToSet = true;
                            remote(null);
                        }
                    }).show();
        }
//        ClientManager.getManager().getClient(mIp).sendQueryFileListCmd("/sdcard/");
//        ClientManager.getManager().getClient(mIp).sendReqFileCmd("/sdcard/faceConfig.txt");
//        ClientManager.getManager().getClient(mIp).sendFileToPath(Config.directoryPath + "/" + "faceConfig.txt","/sdcard/test/faceConfig.txt");
    }

    @Override
    protected void onSelectDir(String path) {
        if (uri != null) {
            mMessages.add(0,"发送文件到远端指定目录： > " + path + "/" + new File(uri.getPath()).getName());
            mAdapter.notifyDataSetChanged();
            ClientManager.getManager().getClient(mIp).sendFileToPath(this,uri,path + "/" + new File(uri.getPath()).getName());
            sendToSet = false;
        }
    }

    @Override
    protected void requestDir(String path) {
        ClientManager.getManager().getClient(mIp).sendQueryFileListCmd(path);
    }

    public void send(View view) {
        ClientSocketHelper helper = ClientManager.getManager().getClient(mIp);
        if (helper != null) {
            String text = mEditText.getText().toString();
            if (!TextUtils.isEmpty(text)) {
                helper.send(text);
                mMessages.add(0,"发送：" + text);
                mAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ClientManager.getManager().removeListener(this);
        EventBus.getDefault().unregister(this);
    }


    @Override
    public void onConnect(String ip, boolean connect) {
        if (!connect) {
            Toast.makeText(this,"连接已断开",Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    protected void handleSelectedFile(Uri uri) {
        super.handleSelectedFile(uri);
        ClientManager.getManager().getClient(mIp).sendFile(this,uri);
    }

    @Override
    protected void deleteRemoteFile(String path) {
        ClientManager.getManager().getClient(mIp).sendDeleteFile(path);
    }

    @Override
    protected void requestFile(String path) {
        ClientManager.getManager().getClient(mIp).sendReqFileCmd(path);
    }

}